library(tidyverse) # for data cleaning and plotting
library(gplots) # for col2hex() function
library(RColorBrewer) # for color palettes
library(sf) # for working with spatial data
library(leaflet) # for highly customizable mapping
library(carData) # for Minneapolis police stops data
library(tidytuesdayR) # for bigfoot data
library(ggthemes) # for more themes (including theme_map())
library(htmltools)
theme_set(theme_minimal())
We are gonna start from the bottokm layer up. The first thing to look it is the basemaps. This means, what will be behind your points or shapes. There are many options and they are super easy to load in. We can start off with the basic basic map, simpple blue ocean and white land, if you zoom in there will be more detials
leaflet() %>%
addTiles()
We can also start the map zoomed in on a specific area using latitude and longitude and the setView() function
Here is Saint Paul MN, now look up your hometown and practice puting in the coordinates.
leaflet() %>%
addTiles() %>%
setView(lng = -93.093124, lat = 44.949642, zoom = 12)
hometown <- leaflet() %>%
setView(lng = -93.093124, lat = 44.949642, zoom = 12) %>%
addTiles()
There are many other basemaps you can use in leaflet! To get different basemaps use the function addProviderTiles(), you will need to know the name of the basemap Here are some examples:
hometown %>%
addProviderTiles(providers$CartoDB.Positron)
hometown %>%
addProviderTiles(providers$Stamen.Watercolor)
hometown %>%
addProviderTiles(providers$CartoDB.DarkMatterNoLabels)
To get the full list of basemaps available through this function click here: http://leaflet-extras.github.io/leaflet-providers/preview/index.html
Let say you really like the water color basemap but there arent any labels, you can layer base maps. Here I have layed the water color with the light grey basemap that had labels. As long as you set the one on top to have a lower opacity you can see both!
hometown %>% addProviderTiles(providers$Stamen.Watercolor) %>%
addProviderTiles(providers$CartoDB.Positron,
options = providerTileOptions(opacity = 0.5))
Now we have our basemap figured out whe can add markers. To do that we will needed data and the observation should have a latitude and longitdue variable to put them on the map.
tuesdata <- tidytuesdayR::tt_load('2022-09-13') #Loading in data from a tidy tuesday that has geographical points
##
## Downloading file 1 of 1: `bigfoot.csv`
bigfoot <- tuesdata$bigfoot
bigfoot